
import axios from "axios";
import Router from "../router";

const instance = axios.create({
    baseURL: 'http://192.168.11.61:9091',
    timeout: 10000,
})

// 添加请求拦截器
instance.interceptors.request.use(function (config) {
    let token = localStorage.getItem('token');
    console.log(token)
    if (token) {
        config.headers = {
            token
        }
    }

    console.log("token ==== ",config.headers)

    if(config.method=='post'||config.method=='put'){
        config.headers['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8'////解决axios提交post问题
    }

    return config;
},function (error) {
    return Promise.reject(error);
});

//添加响应拦截器
instance.interceptors.response.use(function (response) {
    if (response.data.status == 0) {
        return response.data;
    }else if(response.data.status == -10){
        localStorage.removeItem('Authorization')
        Router.push({ name: 'login' })
    }
    return Promise.reject(response.data);    
},function (error) {
    return Promise.reject(error);
})

export default instance;